package snail007;

import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import snail007.bean.ServiceBean;

public class AddService extends PFrame {

	private ServiceBean oldBean;
	private Main main;
	private int row, col;

	/**
	 * Creates new form AddService
	 *
	 * @param bean
	 */
	public AddService(ServiceBean bean, Main main, int row, int col) {
		super();
		oldBean = bean;
		this.main = main;
		this.row = row;
		this.col = col;
		initComponents();
		this.jButton2.setVisible(false);
		if (bean != null) {
			this.setTitle("修改服务");
			this.jTextField1.setText(bean.Name);
			this.jTextArea1.setText(bean.Args);
			this.jTextField4.setText(bean.CertFilePath);
			this.jTextField3.setText(bean.KeyFilePath);
			this.jCheckBox1.setSelected(bean.IsLog);
			this.jCheckBox2.setSelected(bean.AutoStart);
			this.jButton2.setVisible(true);
		} else {
			this.setTitle("添加服务");
		}
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                jFileChooser1 = new javax.swing.JFileChooser();
                jLabel1 = new javax.swing.JLabel();
                jLabel2 = new javax.swing.JLabel();
                jCheckBox1 = new javax.swing.JCheckBox();
                jScrollPane1 = new javax.swing.JScrollPane();
                jTextArea1 = new javax.swing.JTextArea();
                jTextField1 = new javax.swing.JTextField();
                jTabbedPane2 = new javax.swing.JTabbedPane();
                jPanel1 = new javax.swing.JPanel();
                jLabel4 = new javax.swing.JLabel();
                jLabel5 = new javax.swing.JLabel();
                jTextField3 = new javax.swing.JTextField();
                jButton3 = new javax.swing.JButton();
                jTextField4 = new javax.swing.JTextField();
                jButton4 = new javax.swing.JButton();
                jButton1 = new javax.swing.JButton();
                jCheckBox2 = new javax.swing.JCheckBox();
                jButton2 = new javax.swing.JButton();

                setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

                jLabel1.setText("名称");

                jLabel2.setText("参数");

                jCheckBox1.setText("记录日志");

                jTextArea1.setColumns(20);
                jTextArea1.setLineWrap(true);
                jTextArea1.setRows(5);
                jScrollPane1.setViewportView(jTextArea1);

                jTextField1.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                jTextField1ActionPerformed(evt);
                        }
                });

                jLabel4.setText(".crt文件");

                jLabel5.setText(".key文件");

                jButton3.setText("浏览...");
                jButton3.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                jButton3ActionPerformed(evt);
                        }
                });

                jButton4.setText("浏览...");
                jButton4.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                jButton4ActionPerformed(evt);
                        }
                });

                javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
                jPanel1.setLayout(jPanel1Layout);
                jPanel1Layout.setHorizontalGroup(
                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel5))
                                .addGap(8, 8, 8)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                                        .addComponent(jTextField3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap())
                );
                jPanel1Layout.setVerticalGroup(
                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel4)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jButton4))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel5)
                                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jButton3))
                                .addGap(10, 10, 10))
                );

                jTabbedPane2.addTab("tls", jPanel1);

                jButton1.setText("保存");
                jButton1.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                jButton1ActionPerformed(evt);
                        }
                });

                jCheckBox2.setText("自动启动");
                jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                jCheckBox2ActionPerformed(evt);
                        }
                });

                jButton2.setText("删除");
                jButton2.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                jButton2ActionPerformed(evt);
                        }
                });

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addComponent(jLabel2)
                                                                        .addComponent(jLabel1))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jScrollPane1)
                                                                        .addGroup(layout.createSequentialGroup()
                                                                                .addComponent(jCheckBox1)
                                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                                .addComponent(jCheckBox2)
                                                                                .addGap(0, 0, Short.MAX_VALUE))
                                                                        .addComponent(jTextField1)))
                                                        .addComponent(jTabbedPane2)))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(167, 167, 167)
                                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jButton2)))
                                .addContainerGap())
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jCheckBox1)
                                        .addComponent(jCheckBox2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton1)
                                        .addComponent(jButton2))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );

                pack();
                setLocationRelativeTo(null);
        }// </editor-fold>//GEN-END:initComponents

        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
		// TODO add your handling code here:
        }//GEN-LAST:event_jTextField1ActionPerformed

        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
		if (jFileChooser1.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			File f = jFileChooser1.getSelectedFile();
			jTextField3.setText(f.getAbsolutePath());
		}
        }//GEN-LAST:event_jButton3ActionPerformed

        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
		if (jFileChooser1.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			File f = jFileChooser1.getSelectedFile();
			jTextField4.setText(f.getAbsolutePath());
		}
        }//GEN-LAST:event_jButton4ActionPerformed

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		if (!Checker.check(this)) {
			return;
		}
		ServiceBean item = new ServiceBean();
		item.Args = this.jTextArea1.getText().trim();
		item.Name = this.jTextField1.getText().trim();
		item.CertFilePath = this.jTextField4.getText().trim();
		item.KeyFilePath = this.jTextField3.getText().trim();
		item.IsLog = this.jCheckBox1.isSelected();
		item.AutoStart = this.jCheckBox2.isSelected();
		if (row >= 0) {
			item.file = oldBean.file;
		} else {
			String filename = "data/" + String.valueOf(System.currentTimeMillis()) + ".json";
			item.file = new File(filename);
		}
		if (Utils.saveBean(item)) {
			JOptionPane.showMessageDialog(null, "保存成功");
			if (row == -1) {
				DefaultTableModel model = (DefaultTableModel) main.jTable1.getModel();
				model.addRow(new Object[]{item.Name, item.Args, item.IsLog, item.CertFilePath, item.KeyFilePath, "停止", item.AutoStart, "查看", "启动/停止", "修改", item.file});
			} else {
				main.jTable1.setValueAt(item.Name, row, 0);
				main.jTable1.setValueAt(item.Args, row, 1);
				main.jTable1.setValueAt(item.IsLog, row, 2);
				main.jTable1.setValueAt(item.CertFilePath, row, 3);
				main.jTable1.setValueAt(item.KeyFilePath, row, 4);
				main.jTable1.setValueAt(item.AutoStart, row, 6);
			}
			this.dispose();
		} else {
			JOptionPane.showMessageDialog(null, "保存失败");
		}

        }//GEN-LAST:event_jButton1ActionPerformed

        private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
		// TODO add your handling code here:
        }//GEN-LAST:event_jCheckBox2ActionPerformed

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
		DefaultTableModel model = (DefaultTableModel) main.jTable1.getModel();
		((File) main.jTable1.getValueAt(row, 10)).delete();
		model.removeRow(row);
		this.dispose();
        }//GEN-LAST:event_jButton2ActionPerformed


        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        public javax.swing.JCheckBox jCheckBox1;
        public javax.swing.JCheckBox jCheckBox2;
        private javax.swing.JFileChooser jFileChooser1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTabbedPane jTabbedPane2;
        public javax.swing.JTextArea jTextArea1;
        public javax.swing.JTextField jTextField1;
        public javax.swing.JTextField jTextField3;
        public javax.swing.JTextField jTextField4;
        // End of variables declaration//GEN-END:variables
}
